// rs_anwser/pages/subject/subject.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    collectPrompt: true,
    animation: {},
    current: 0,
    pageSize: 10,
    wrongCount: 0,
    rightCount: 0,
    total: 160,
    subjectList: [
    ],
    param: { subjectType: 'mutilple', userId: app.getUserId() }
  
  },

  

  changeSubject:function(e){
    this.setData({ current: e.detail.current });
    // if (e.detail.current == this.data.subjectList.length - 1) {
    //   app.getData(this, "entry/wxapp/getWrongList", { subjectType: 'mutilple', userId: app.getUserId() });
    // }
  
  },
  updateCurrent:function(data){
    this.setData({ current: data.detail });
  },
  updateSubjectList: function (data) {
    this.setData({subjectList:data.detail});

  },
  updatePageIndex:function(data){
    this.setData({ pageIndex: data.detail });
  },

  /**
    * 选择题目选项
    */
  selectOption: function (e) {


    var subject = app.subjectPage.getCurrentSubject(this);
    if (typeof (subject.result) == "undefined") {
      //如果还没有没有答题
      var answer = subject.anwser;
      var selectOption = e.currentTarget.dataset.label;
      var index = e.currentTarget.dataset.index;
      var options = subject.options;
      var selectedOptions = subject.selectedOptions;
      selectedOptions = selectedOptions ? selectedOptions.split(',') : [];

      if (selectOption == options[index].label && selectedOptions.indexOf(selectOption) == -1) {
        options[index]['resultClass'] = 'result-right';
        selectedOptions.push(options[index].label);

      }

      subject.options = options;
      subject.selectedOptions = selectedOptions.join();
      app.subjectPage.updateCurrentSubject(this, subject);
    }
  },

  /**
   * 确定选择的答案
   */
  confirm: function (e) {
    var subject = app.subjectPage.getCurrentSubject(this);
    if (!subject.result) {
      var selectedOptions = subject.selectedOptions.split(',');
      var answer = subject.anwser.split(',');
      if (selectedOptions.sort().toString() == answer.sort().toString()) {
        subject.result = 1;
        this.setData({ rightCount: ++this.data.rightCount });
        subject.resultColor = 'color:green';
        wx.showToast({
          title: '答题正确',
        })
      } else {
        subject.result = 2;
        this.setData({ wrongCount: ++this.data.wrongCount });
        subject.resultColor = 'color:red';
        app.subjectPage.updateCurrentSubject(this, subject);
    }
    }




  },

  /**
   * 
   */
  collect:function(){

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    app.getData(this, "entry/wxapp/getWrongList", { subjectType: 'mutilple', userId: app.getUserId() });
    
   
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.selectComponent('#collect_promt').startPrompt();
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  /**
   * 获取题目
   */
  getCurrentSubject:function(){

    var subjectList = this.data.subjectList;
    var current = this.data.current;
    return subjectList[current];

  },

   /**
    * 更新当前题目状态
    */
  updateCurrentSubject:function(subject){
    var subjectList = this.data.subjectList;
    var current = this.data.current;
    subjectList[current] = subject;
    this.setData({subjectList:subjectList});
  }
})